package com.glorysoft.hlgdpda.ui.setting.viewmodel

import android.app.Activity
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import com.glorysoft.hlgdpda.base.BaseViewModel
import com.glorysoft.hlgdpda.base.MyApp
import com.glorysoft.hlgdpda.base.Constant
import com.glorysoft.hlgdpda.entity.ui.ToastMessageBean
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils

/**
 * FileName: SettingViewModel
 * Author: XJH
 * Date: 2021/5/13 14:59
 * Description:
 * History:
 */
class SettingViewModel : BaseViewModel(){

    var constant = MutableLiveData<Constant.Companion>()
    private var dialog: FreeUI_GeneralFragmentDialog? = null

    fun initVersion(context: Activity){
        constant.value = Constant.Companion
        val manager: PackageManager = context.packageManager
        var info: PackageInfo? = null
        try {
            info = manager.getPackageInfo(context.packageName, 0)
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        val version = info!!.versionName
        constant.value?.VRESION = version
    }

    /**
     * type 0代表REQUEST_URL_DEBUG  1 代表FILE_URL
     */
    fun showScanDialog(context: AppCompatActivity,type : Int){
        dialog = FreeApi_DialogUtils.getScanDialog(context, if(type == 0)"请输入服务器地址" else "请输入文件地址",
                if(type == 0) Constant.REQUEST_URL_DEBUG else Constant.FILE_URL) {

            if (type == 0) constant.value?.REQUEST_URL_DEBUG = it else constant.value?.FILE_URL = it

            defUI.toastEvent.value = ToastMessageBean("保存成功", true)
            FreeApi_SharePreferencesUtils.setSharePre("HLGD_FILE", if(type == 0)"REQUEST_URL_DEBUG" else "FILE_URL_DEBUG", it, MyApp.app)
            dialog?.dismiss()
        }
        dialog?.show(context.supportFragmentManager, "WEB_URL_DIALOG")
    }
}